package com.pie4cloud.pie.job.biz.gather.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.log.annotation.SysLog;
import com.pie4cloud.pie.job.api.entity.ConfigFtp;
import com.pie4cloud.pie.job.api.utils.FtpUtils;
import com.pie4cloud.pie.job.biz.gather.service.ConfigFtpService;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 
 *
 * @author feilei
 * @date 2022-01-10 00:54:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/configftp" )
@Api(value = "configftp", tags = "管理")
public class ConfigFtpController {

    private final ConfigFtpService configFtpService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param configFtp 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('gather_configftp_get')" )
    public R geconfigFtpPage(Page page, ConfigFtp configFtp) {
        return R.ok(configFtpService.page(page, Wrappers.query(configFtp)));
    }

	/**
	 * @return
	 */
	@ApiOperation(value = "查询所有", notes = "查询所有")
	@GetMapping("/list" )
	//@PreAuthorize("@pms.hasPermission('gather_configftp_get')" )
	public R<List<ConfigFtp>> list() {
		return R.ok(configFtpService.list());
	}


    /**
     * 通过id查询
     * @param ftpId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{ftpId}" )
    @PreAuthorize("@pms.hasPermission('gather_configftp_get')" )
    public R getById(@PathVariable("ftpId" ) Long ftpId) {
        return R.ok(configFtpService.getById(ftpId));
    }

    /**
     * 新增
     * @param configFtp 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('gather_configftp_add')" )
    public R save(@RequestBody ConfigFtp configFtp) {
		boolean save = configFtpService.save(configFtp);
		if (save) {
			return R.ok(configFtp);
		}else  {
			return R.failed("新增FTP信息失败");
		}
    }

	/**
	 * 检查FTP状态
	 * @param configFtp
	 * @return R
	 */
	@ApiOperation(value = "检查FTP状态", notes = "检查FTP状态")
	@SysLog("检查FTP状态" )
	@PostMapping("/checkFtpStatus")
	public R checkFtpStatus(@RequestBody ConfigFtp configFtp) {
		FTPClient ftpClient = new FTPClient();
		boolean connect = FtpUtils.connect(ftpClient, configFtp.getFtpAddress(), configFtp.getFtpPort(), configFtp.getFtpUserName(), configFtp.getFtpPwd());
		if(connect) {
			FtpUtils.disconnect(ftpClient);
			return R.ok("连接成功！");
		}else {
			return R.failed("连接失败！");
		}

	}

    /**
     * 修改
     * @param configFtp 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('gather_configftp_edit')" )
    public R updateById(@RequestBody ConfigFtp configFtp) {
        return R.ok(configFtpService.updateById(configFtp));
    }

    /**
     * 通过id删除
     * @param ftpId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{ftpId}" )
    @PreAuthorize("@pms.hasPermission('gather_configftp_del')" )
    public R removeById(@PathVariable Long ftpId) {
        return R.ok(configFtpService.removeById(ftpId));
    }

}
